#include "PictureAutomation.h"
#include <QPixmap>

#include "IF_PictureEngineInterface.h"
#include "MediaListModel.h"
#include "ActionListController.h"
#include "PictureSource.h"


PictureAutomation::PictureAutomation( IF_PictureEngineInterface * engine, MediaListModel * model,
                                      ActionListController * controller, QObject * parent):
   QObject(parent),
   m_engine( engine),
   m_model( model),
   m_controller( controller)
{
}

PictureAutomation::~PictureAutomation()
{

}

void PictureAutomation::activateByRowNumer( int activeRow)
{
   /* check if a track has been activated */
   if (activeRow >= 0)
   {
      /* valid track activated */
      QModelIndex index = m_model->index(activeRow);
      const AbstractMediaSource *source = m_model->mediaData( index);

      /* if source is not an image, this will be NULL */
      const PictureSource * pictureSource = dynamic_cast<const PictureSource *>(source);

      if (pictureSource != NULL)
      {
         m_engine->setCurrentSource( pictureSource->mediaContent().value<QPixmap>());
      }
      else
      {
         m_engine->setCurrentSource( QPixmap());
      }
   }
   else
   {
      /* no active entry: it might have been erased */
      m_engine->setCurrentSource( QPixmap());
   }
}


void PictureAutomation::activateById(const QString &stringId, bool playImmediately)
{
   QModelIndex target = m_model->searchById( stringId);

   if (target != QModelIndex())
   {
      m_controller->activateRequest( target.row());

      if (playImmediately)
      {
         m_engine->show();
      }
   }
}

